package daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import vo.Customer;
import dao.BaseDAO;
import dao.ICustomerDao;

public class CustomerDao extends BaseDAO implements ICustomerDao {
	   //======1.显示商品=============

	public List getCustomer(int page,int rows,String hql) {
		Session session=getSession();
		Query query=session.createQuery(hql);
		int startRow=(page-1)*rows;
		query.setFirstResult(startRow);
		query.setMaxResults(rows);
		
		List customer=query.list();
		
		session.close();
		return customer;
	}
		//======2.添加商品============
	public void addCustomer(Customer customer){
		Session session=getSession();
		Transaction tx=session.beginTransaction();
		session.save(customer);
		tx.commit();
		session.close();
	}
		//======3.删除商品============
	public void delCustomer(int customerId){
		Session session=getSession();
		System.out.println("删除成功");
		String hql="delete Customer  where CustomerId="+customerId;
		Query query=session.createQuery(hql);
		
	query.executeUpdate();
		System.out.println("删除成功");
		
		session.close();
	}
		
		//======4.编辑商品============
public void editCustomer(Customer customer){
		
		
		
		
		Session session = this.getSession();
		Transaction tc = (Transaction) session.beginTransaction();
		session.update(customer);
		try {
			tc.commit();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		session.close();
		
		
	}
	
		//======5.查询商品============
      
		
		//======6.获得所有的商品数============
      public int  getAllCustomer(String hql){
    	  Session session=getSession();
    	  Query query=session.createQuery(hql);
    	  List customer=query.list();
  		
  		session.close();
  		return customer.size();
    	  
      }
	

}
